from django.db import models
from django.contrib.auth.models import User


class FlowerCategory(models.Model):
    """鲜花分类"""
    name = models.CharField(max_length=100, verbose_name="分类名称")
    description = models.TextField(blank=True, null=True, verbose_name="分类描述")
    image = models.URLField(blank=True, null=True, verbose_name="分类图片")
    sort_order = models.IntegerField(default=0, verbose_name="排序")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'flower_category'
        verbose_name = '鲜花分类'
        verbose_name_plural = '鲜花分类'
        ordering = ['sort_order', '-created_at']

    def __str__(self):
        return self.name


class Flower(models.Model):
    """鲜花商品"""
    STATUS_CHOICES = [
        ('active', '上架'),
        ('inactive', '下架'),
        ('out_of_stock', '缺货'),
    ]

    name = models.CharField(max_length=200, verbose_name="鲜花名称")
    name = models.CharField(max_length=200, verbose_name="鲜花名称")
    category = models.ForeignKey(FlowerCategory, on_delete=models.CASCADE, verbose_name="分类")
    description = models.TextField(verbose_name="商品描述")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格")
    original_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name="原价")
    image = models.URLField(blank=True, null=True, verbose_name="主图URL")
    stock = models.IntegerField(default=0, verbose_name="库存数量")
    sales_count = models.IntegerField(default=0, verbose_name="销售数量")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active', verbose_name="状态")
    sort_order = models.IntegerField(default=0, verbose_name="排序")
    is_featured = models.BooleanField(default=False, verbose_name="是否推荐")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'flower'
        verbose_name = '鲜花商品'
        verbose_name_plural = '鲜花商品'
        ordering = ['sort_order', '-created_at']

    def __str__(self):
        return self.name

    @property
    def main_image(self):
        """获取主图"""
        image = self.images.filter(is_main=True).first()
        return image.image_url if image else None

    @property
    def image_list(self):
        """获取所有图片"""
        return [img.image_url for img in self.images.all()]


class FlowerImage(models.Model):
    """鲜花图片"""
    flower = models.ForeignKey(Flower, related_name='images', on_delete=models.CASCADE, verbose_name="鲜花")
    image_url = models.URLField(verbose_name="图片URL")
    is_main = models.BooleanField(default=False, verbose_name="是否主图")
    sort_order = models.IntegerField(default=0, verbose_name="排序")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = 'flower_image'
        verbose_name = '鲜花图片'
        verbose_name_plural = '鲜花图片'
        ordering = ['sort_order', 'created_at']

    def __str__(self):
        return f"{self.flower.name} - 图片"


class FlowerSpec(models.Model):
    """鲜花规格"""
    flower = models.ForeignKey(Flower, related_name='specs', on_delete=models.CASCADE, verbose_name="鲜花")
    name = models.CharField(max_length=100, verbose_name="规格名称")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="规格价格")
    stock = models.IntegerField(default=0, verbose_name="库存数量")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    sort_order = models.IntegerField(default=0, verbose_name="排序")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        db_table = 'flower_spec'
        verbose_name = '鲜花规格'
        verbose_name_plural = '鲜花规格'
        ordering = ['sort_order', 'created_at']

    def __str__(self):
        return f"{self.flower.name} - {self.name}"